package com.uff.hmstpa.model.service.impl;

import java.util.List;

import org.joda.time.DateTime;

import com.uff.hmstpa.model.decorator.IAppointment;
import com.uff.hmstpa.model.domain.Appointment;
import com.uff.hmstpa.model.domain.Doctor;
import com.uff.hmstpa.model.domain.Patient;
import com.uff.hmstpa.model.mediator.ModelMediator;
import com.uff.hmstpa.model.persistence.AppointmentDAO;
import com.uff.hmstpa.model.persistence.impl.AppointmentDAOImpl;
import com.uff.hmstpa.model.service.AppointmentService;
import com.uff.hmstpa.util.exception.BusinessRuleException;

public class AppointmentServiceImpl implements AppointmentService {

	private ModelMediator mediator;
	private AppointmentDAO appointmentDAO = AppointmentDAOImpl.getInstance();

	public AppointmentServiceImpl() {
	}

	public List<Appointment> retrieveAppointmentList(Doctor doctor) {
		if (doctor != null) {
			return appointmentDAO.loadAppointments(doctor.getCRM());
		} else {
			return appointmentDAO.loadAllAppointments();
		}
	}

	public Appointment retrieveAppointment(Doctor doctor, Patient patient, DateTime time) {
		return appointmentDAO.retrieve(doctor.getCRM(), patient.getCpf(), time);
	}

	public Appointment createAppointment(Doctor doctor, Patient patient, DateTime time, IAppointment app) throws BusinessRuleException {
		Appointment appointment = null;

		//Verifica se a data não é menor do que a atual (regra de negócio)
		if (time.compareTo(DateTime.now()) <= 0) {
			throw new BusinessRuleException(this, "Data do agendamento deve ser posterior a data atual.");
		} else {
			appointment = retrieveAppointment(doctor, patient, time);
			if (appointment == null) {
				appointmentDAO.persist(doctor.getCRM(), patient.getCpf(), time, app.getPrice());
			}
		}

		appointment = retrieveAppointment(doctor, patient, time);

		return appointment;
	}

	public void removeAppointment(String crm, String cpf, String time) {
		appointmentDAO.delete(crm, cpf, time);

	}

	public void setMediator(ModelMediator mediator) {
		this.mediator = mediator;
	}
	
	public ModelMediator getMediator() {
		return this.mediator;
	}
}
